package com.Blog.www.service.Impl;

import com.Blog.www.constant.PageConstant;
import com.Blog.www.constant.TipsConstant;
import com.Blog.www.dao.FriendDao;
import com.Blog.www.dao.Impl.FriendDaoImpl;
import com.Blog.www.service.FriendService;
import com.Blog.www.utils.DataExamine;

import javax.servlet.http.HttpServletRequest;

/**
 * 作者关注类
 *
 * @author 君未洋
 */
public class FriendServiceImpl implements FriendService {

    private static FriendDao dao = new FriendDaoImpl();
    private static DataExamine data = new DataExamine();

    @Override
    public Boolean findFriend(int uid, int aid) {
        if (uid == aid) {
            return false;
        }
        Integer count = dao.findFriend(aid, uid);
        return (count > 0);
    }

    @Override
    public HttpServletRequest updateFriend(Integer status, Integer id, Integer aid, HttpServletRequest req) {
        req.removeAttribute(PageConstant.USER_AUTHOR);
        if (data.numberExamine(id + "") || data.numberExamine(id + "") || data.numberExamine(status + "")) {
            req.setAttribute(PageConstant.USER_AUTHOR, TipsConstant.USER_AUTHOR_A);
        } else if (id.equals(aid)) {
            req.setAttribute(PageConstant.USER_AUTHOR, TipsConstant.USER_AUTHOR_B);
        } else if (status == 0) {
            //是执行删除操作
            if (dao.delete(aid, id)) {
                req.setAttribute(PageConstant.USER_AUTHOR, TipsConstant.USER_AUTHOR_D);
            } else {
                req.setAttribute(PageConstant.USER_AUTHOR, TipsConstant.BLOG_OPERATION_A);
            }
        } else if (status == 1) {
            if (dao.add(aid, id)) {
                req.setAttribute(PageConstant.USER_AUTHOR, TipsConstant.USER_AUTHOR_C);
            } else {
                req.setAttribute(PageConstant.USER_AUTHOR, TipsConstant.BLOG_OPERATION_A);
            }
        }
        return req;
    }

    @Override
    public Integer friendCount(Integer aid) {
        if (data.numberExamine(aid + "")) {
            return null;
        }
        return dao.getCount(aid);
    }
}
